package clevo.common.code.helper.fsm;


import clevo.common.BaseObject;
import clevo.common.code.helper.Writeable;
import org.apache.velocity.VelocityContext;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

public class StatGenerator extends BaseObject implements Writeable {
    private static final long serialVersionUID = 1L;

    private String pkg;
    private String name;
    private List<String> actions = new ArrayList<String> ();
    private List<Stat> stats = new ArrayList<Stat>();

    public StatGenerator addStat(Stat s) {
        stats.add(s);
        s.getActions().forEach((a, t) -> {
            if (!actions.contains(a)) {
                actions.add(a);
            }
        });
        return this;
    }

    public void write(String out) {
        String path = out.endsWith(File.separator) ? out : out + File.separator;
        path = path + pkg.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
        path = path.endsWith(File.separator) ? path : path + File.separator;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        VelocityContext vc = new VelocityContext();
        vc.put("s", this);
        write(vc, "template/fsm/State.java", path + name + "State.java");
        write(vc, "template/fsm/States.java", path + name + "States.java");
        write(vc, "template/fsm/Status.java", path + name + "Status.java");
        write(vc, "template/fsm/Context.java", path + name + "Context.java");
        write(vc, "template/fsm/DftContext.java", path + "Default" + name + "Context.java");
    }

    public String getPkg() {
        return pkg;
    }

    public void setPkg(String pkg) {
        this.pkg = pkg;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getActions() {
        return actions;
    }

    public void setActions(List<String> actions) {
        this.actions = actions;
    }

    public List<Stat> getStats() {
        return stats;
    }

    public void setStats(List<Stat> stats) {
        this.stats = stats;
    }

}
